package cn.yunsenlin.formula.cloud.result;


import cn.yunsenlin.formula.cloud.auth.AuthRRH;
import cn.yunsenlin.formula.cloud.bean.result.Result;
import cn.yunsenlin.formula.cloud.bean.result.ResultRequest;
import cn.yunsenlin.formula.cloud.bean.template.TemplateTest;
import cn.yunsenlin.formula.cloud.interfaces.AuthService;
import cn.yunsenlin.formula.cloud.interfaces.ResultService;
import cn.yunsenlin.formula.cloud.mould.ControllerMould;
import cn.yunsenlin.formula.cloud.mysql.bean.generator.TbResultContent;
import cn.yunsenlin.formula.cloud.responsibility.ContinueAbleRRH;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/result")
@CrossOrigin
public class ResultController {
    private final AuthRRH authRRH;
    private final ResultService resultService;
    private final AuthService authService;

    public ResultController(AuthRRH authRRH, ResultService resultService, AuthService authService) {
        this.authRRH = authRRH;
        this.resultService = resultService;
        this.authService = authService;
    }

    /**
     * 计算并存储
     *
     * @param request  计算请求
     * @param response response
     * @param session  session
     * @return 计算结果
     */
    @RequestMapping(path = "/", method = RequestMethod.PUT)
    public Result analyze(@RequestBody ResultRequest request, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return resultService.analyze(request);
            }
        }) {
        }.startUpObject(response, session);
    }

    /**
     * 修改计算结果
     *
     * @param request  计算请求
     * @param response response
     * @param session  session
     * @return 修改后的计算结果
     */
    @RequestMapping(path = "/", method = RequestMethod.PATCH)
    public Result modify(@RequestBody ResultRequest request, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return resultService.modify(request);
            }
        }) {
        }.startUpObject(response, session);
    }

    /**
     * 移动计算结果
     *
     * @param resultId 计算结果ID
     * @param groupId  新计算组ID
     * @param response response
     * @param session  session
     * @return 移动后的计算结果
     */
    @RequestMapping(path = "/move/{resultId}/{groupId}", method = RequestMethod.PATCH)
    public Result move(@PathVariable("resultId") int resultId, @PathVariable("groupId") int groupId, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return resultService.moveGroup(resultId, groupId);
            }
        }) {
        }.startUpObject(response, session);
    }

    /**
     * 删除计算记录
     *
     * @param id       计算记录ID
     * @param response response
     * @param session  session
     */
    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    public void remove(@PathVariable("id") int id, HttpServletResponse response, HttpSession session) {
        new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                resultService.remove(id);
                return null;
            }
        }) {
        }.startUpObject(response, session);
    }

    /**
     * 查询计算记录
     *
     * @param groupId    计算组ID
     * @param templateId 公式ID
     * @param response   response
     * @param session    session
     * @return 计算记录列表
     */
    @RequestMapping(path = "/{groupId}/{templateId}", method = RequestMethod.POST)
    public List<Result> select(@PathVariable("groupId") Integer groupId, @PathVariable("templateId") Integer templateId, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return resultService.selectByGroupIdAndTemplateId(groupId, templateId);
            }
        }) {
        }.startUpArray(response, session);
    }

    /**
     * 检查输入合法性
     *
     * @param templateId  公式ID
     * @param symbol      输入项符号
     * @param contentList 输入列表
     * @param response    response
     * @param session     session
     * @return 输入合法性
     */
    @RequestMapping(path = "checkValidWithTemplateId/{templateId}/{symbol}", method = RequestMethod.POST)
    public String checkValidWithTemplateId(@PathVariable("templateId") Integer templateId, @PathVariable("symbol") String symbol, @RequestBody List<TbResultContent> contentList, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return resultService.checkValidWithTemplateId(contentList, templateId, symbol);
            }
        }) {
        }.startUpObject(response, session);
    }

    /**
     * 检查测试公式测试合法性
     *
     * @param symbol       输入项符号
     * @param templateTest 公式测试信息
     * @param response     response
     * @param session      session
     * @return 输入合法性
     */
    @RequestMapping(path = "checkValidByTemplateTest/{symbol}", method = RequestMethod.POST)
    public String checkValidByTemplateTest(@PathVariable("symbol") String symbol, @RequestBody TemplateTest templateTest, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return resultService.checkValidByTemplateTest(templateTest, symbol, authService.selectUserBySession(session).getId());
            }
        }) {
        }.startUpObject(response, session);
    }

    /**
     * 获取测试的计算结果
     *
     * @param templateTest 公式模板测试输入
     * @param response     response
     * @param session      session
     * @return 计算结果
     */
    @RequestMapping(path = "testTemplate/", method = RequestMethod.POST)
    public Result testTemplate(@RequestBody TemplateTest templateTest, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return resultService.templateTest(templateTest);
            }
        }) {
        }.startUpObject(response, session);
    }
}
